<template>
  <div id="home">
    <!-- 顶部 -->
    <div class="top">
      <div class="address" @click="changeAddress">
        <img
          src="https://tsyx-rights-and-interests.oss-cn-hangzhou.aliyuncs.com/icon/address.png"
          alt=""
        />
        <span>{{ cityName }}</span>
      </div>
      <div class="search" @click="toSearch">
        <!-- <van-icon name="search" /> -->
        <img
          src="https://tsyx-rights-and-interests.oss-cn-hangzhou.aliyuncs.com/icon/search.png"
          alt=""
        />
        快速查找产品
      </div>
      <div class="signIn" @click="toSignIn">
        <img
          src="https://tsyx-rights-and-interests.oss-cn-hangzhou.aliyuncs.com/icon/signIn.png"
          alt=""
        />
      </div>
    </div>
    <!-- 会员盒子 -->
    <div class="memberBox">
      <!-- 会员卡 -->
      <div :class="isright ? 'member ismember' : 'member nomember'">
        <img
          src="https://tsyx-rights-and-interests.oss-cn-hangzhou.aliyuncs.com/background/logo.png"
          alt=""
          class="logo"
        />
        <div
          :class="isright ? 'watchAll' : 'watchAll noColor'"
          @click="watchAllOrMore(isright)"
        >
          {{ isright ? "查看全部" : "更多权限" }}
          <div
            :class="isright ? 'icon-v-right' : 'icon-v-right noBorderColor'"
          ></div>
        </div>
        <div class="time" v-show="isright">{{ userMsg.vipEnd }}到期</div>
        <div
          v-if="!isright"
          :class="isright ? 'memberButton' : 'memberButton nobg'"
          @click="watchAllOrMore(isRight)"
        >
          {{ isright ? "续费VIP" : "点击领取" }}
        </div>
      </div>
      <div class="memberTab">
        <van-row gutter="20">
          <van-col
            span="6"
            v-for="(item, key) in memberList"
            :key="key"
            :style="
              isright
                ? 'text-align: center;'
                : 'text-align: center;color: #CBCBCB;'
            "
            @click="memberToOtherPage(item.productUrl)"
          >
            <van-badge :content="item.num" class="tabs">
              <img :src="item.productIcon" alt="" />
              <p>{{ item.productName }}</p>
            </van-badge>
          </van-col>
        </van-row>
      </div>
    </div>
    <!-- 轮播图 -->
    <div class="banner">
      <van-swipe
        class="my-swipe"
        :autoplay="3000"
        indicator-color="white"
        style="width: 100%"
      >
        <van-swipe-item
          v-for="(item, key) in bannerList"
          :key="key"
          @click="bannerToOtherPage(item)"
          class="bannerImg"
        >
          <img :src="item.adPicture" alt="" />
        </van-swipe-item>
      </van-swipe>
    </div>
    <!-- 热门商品轮播 -->
    <h2 class="hotShopTitle">热门推荐</h2>
    <div class="hotShopBox">
      <div
        class="hotShop"
        v-for="(item, key) in hotShopList"
        :key="key"
        @click="hotShopBoxClick(item)"
      >
        <!-- <van-badge :content="item.adLabel" class="hotBadge youlike"> -->
        <van-badge :content="item.asdasd" class="hotBadge youlike">
          <img :src="item.adPicture" alt="" />
        </van-badge>
        <h3 class="title">{{ item.adName }}</h3>
        <p class="msg">
          {{ item.adDescribe }}
        </p>
        <div class="price">
          <span>￥</span>
          <i>{{ item.presentPrice }}</i>
        </div>
      </div>
    </div>
    <!-- 热门推荐等 -->
    <van-tabs v-model="active" scrollspy sticky class="hotList">
      <van-tab
        v-for="(item, index) in productMap"
        :title="item.classifyName"
        :key="index"
        style=""
        class="hotBigBox"
      >
        <div
          class="box"
          v-for="value in item.list"
          :key="value.id"
          @click="hotListClick(value)"
        >
          <van-badge :content="value.productLabel" class="hotBadge">
            <img :src="value.productIcon" alt="" />
            <div class="fiveSela" v-if="value.productDescribe != ''">
              {{ value.productDescribe }}
            </div>
          </van-badge>
          <span>{{ value.productName }}</span>
        </div>
      </van-tab>
    </van-tabs>
  </div>
</template>

<script>
import req from "@/utils/index";
import { hostName } from "@/utils/config";
const wx = require("weixin-js-sdk");
import { Toast, Dialog } from "vant";
export default {
  name: "home",
  components: {},
  data() {
    return {
      isright: false,
      active: "1",
      //   会员权益
      memberList: [],
      //   轮播图
      bannerList: [],
      //   热门商品
      hotShopList: [],
      // 热门推荐
      productMap: [],
      // 定位
      cityName: window.localStorage.getItem("cityName"),
      userMsg: {},
    };
  },
  created() {
    this.cityName = window.localStorage.getItem("cityName") || "定位中";
    this.isright = this.$store.state.isright;
    if (this.$route.query.channelNo) {
      window.localStorage.setItem("channelNo", this.$route.query.channelNo);
    }
    // 获取首页
    req("get", "/qyHome/getHome", {}).then((res) => {
      if (res.rspCode == "000000") {
        this.memberList = res.data.topList;
        this.bannerList = res.data.firstAdList;
        this.hotShopList = res.data.secondAdList;
        this.productMap = res.data.productMap;
      }
    });
    // 获取wxJSSdk
    req(
      "get",
      `/wx/service/api/qy/getJssdkParams?url=${encodeURIComponent(
        hostName + this.$route.fullPath
      )}`,
      {}
    ).then((res) => {
      // console.log(res)
      if (res.rspCode == "000000") {
        if (!res.data) return;
        // const { appId, nonceStr, signature, timestamp } = res.data;
        wx.config({
          debug: false, // 开启调试模式,调用的所有api的返回值会在客户端alert出来，若要查看传入的参数，可以在pc端打开，参数信息会通过log打出，仅在pc端时才会打印。
          appId: res.data.appId, // 必填，公众号的唯一标识
          timestamp: res.data.timestamp, // 必填，生成签名的时间戳
          nonceStr: res.data.nonceStr, // 必填，生成签名的随机串
          signature: res.data.signature, // 必填，签名
          jsApiList: ["updateAppMessageShareData"], // 必填，需要使用的JS接口列表
        });
      }
    });
    // 获取9.9Vip会员
    req("get", "/qyHome/getVipGoods?goodsType=3", {}).then((res) => {
      // console.log(res)
      window.localStorage.setItem("VipGoodsNo", res);
    });
    // 获取当前渠道支付方式
    req(
      "get",
      `/qyHome/getPayType?channelNo=${window.localStorage.getItem(
        "channelNo"
      )}`,
      {}
    ).then((res) => {
      window.localStorage.setItem(
        "payModeList",
        JSON.stringify(res.data.payModeList[0])
      );
    });

    const userMsg = JSON.parse(window.localStorage.getItem("userMsg"));
    if (!userMsg) {
      return;
    }
    this.userMsg = userMsg;
    req("get", `/qy/user/info`, {}).then((res) => {
      // console.log(res);
      if (res.rspCode == "000000") {
        this.isLogin = true;
        this.userMsg = res.data;
        window.localStorage.setItem("userMsg", JSON.stringify(res.data));
        if (res.data.userType == 0) {
          this.$store.state.isright = false;
        } else {
          this.$store.state.isright = true;
        }
      } else {
        this.$store.state.isright = false;
        this.isLogin = false;
      }
    });
    if (userMsg.userType == 0) {
      this.isright = this.$store.state.isright = false;
    } else if (userMsg.userType == 1 || userMsg.userType == 2) {
      this.isright = this.$store.state.isright = true;
    }
  },
  methods: {
    //   改变定位
    changeAddress() {
      this.cityName = window.localStorage.getItem("cityName") || "定位中...";
    },
    //   去搜索页面
    toSearch() {
      this.$router.push({ name: "search" });
    },
    //   去签到
    toSignIn() {
      Toast("暂未开放~");
    },
    // 会员用券等
    memberToOtherPage(id) {
      if (id == 1) {
        // if (!this.isright) return;
        if (this.userMsg.userType == 1 || this.userMsg.userType == 2) {
          this.$store.state.indexCardStatus = id;
          this.$router.push({
            name: "indexCard",
            params: { id },
          });
        } else {
          this.$router.push({
            name: "youQuanMsg",
          });
        }
      } else if (id == 4) {
        this.$store.state.indexCardStatus = id;
        if (this.userMsg.firstBuyWater == 0) {
          this.$router.push({
            name: "indexCard",
            params: { id },
          });
        } else {
          this.$router.push({
            name: "posterNo1",
          });
        }
      } else {
        Toast("暂未开放~");
        return;
      }
    },
    // 轮播图去别的页面
    bannerToOtherPage(item) {
      // console.log(item)
      if (item.adUrl.substring(0, 4) == "http") {
        window.location.href = item.adUrl + `&tsToken=${window.localStorage.getItem('logintoken')}`;
      } else {
        const vipGoodsNo = window.localStorage.getItem("VipGoodsNo");
        if (this.userMsg.userType == 1 && item.adUrl == vipGoodsNo) {
          Dialog.confirm({
            message:
              "您的手机号已经是平台的会员，但是暂未认证，是否前去认证领取更多权益？",
          })
            .then(() => {
              // on confirm
              this.$router.push({
                name: "upload",
              });
            })
            .catch(() => {
              // on cancel
            });
          return;
        }
        if (this.userMsg.userType == 2 && item.adUrl == vipGoodsNo) {
          Toast("您已经是尊贵的湖南中石油会员啦~");
          return;
        }
        if (this.userMsg.userType == 0 && item.adUrl == vipGoodsNo) {
          window.localStorage.setItem("whatBuy", "vip");
          this.$router.push({
            name: "creatOrder",
            query: {
              goodsNo: item.adUrl,
              didPay: "noYet",
            },
          });
          return;
        }
        this.$router.push({
          name: "creatOrder",
          query: {
            goodsNo: item.adUrl,
            didPay: "noYet",
          },
        });
      }
    },
    // 猜你喜欢
    hotShopBoxClick(item) {
      // console.log(item)
      if (item.adUrl.substring(0, 4) == "http") {
        // console.log(1)
        window.location.href = item.adUrl + `&tsToken=${window.localStorage.getItem('logintoken')}`;
      } else {
        const vipGoodsNo = window.localStorage.getItem("VipGoodsNo");
        if (this.userMsg.userType == 1 && item.adUrl == vipGoodsNo) {
          Dialog.confirm({
            message:
              "您的手机号已经是平台的会员，但是暂未认证，是否前去认证领取更多权益？",
          })
            .then(() => {
              // on confirm
              this.$router.push({
                name: "upload",
              });
            })
            .catch(() => {
              // on cancel
            });
          return;
        }
        if (this.userMsg.userType == 2 && item.adUrl == vipGoodsNo) {
          Toast("您已经是尊贵的湖南中石油会员啦~");
          return;
        }
        if (this.userMsg.userType == 0 && item.adUrl == vipGoodsNo) {
          window.localStorage.setItem("whatBuy", "vip");
          this.$router.push({
            name: "creatOrder",
            query: {
              goodsNo: item.adUrl,
              didPay: "noYet",
            },
          });
          return;
        }
        this.$router.push({
          name: "creatOrder",
          query: {
            goodsNo: item.adUrl,
            didPay: "noYet",
            isGsq: false,
          },
        });
      }
    },
    // 热门推荐
    hotListClick(item) {
      // console.log(item)
      if (item.productUrl.substring(0, 4) == "http") {
        window.location.href = item.productUrl + `&tsToken=${window.localStorage.getItem('logintoken')}`;
        // console.log(item.productUrl)
      } else {
        const vipGoodsNo = window.localStorage.getItem("VipGoodsNo");
        if (this.userMsg.userType == 1 && item.adUrl == vipGoodsNo) {
          Dialog.confirm({
            message:
              "您的手机号已经是平台的会员，但是暂未认证，是否前去认证领取更多权益？",
          })
            .then(() => {
              // on confirm
              this.$router.push({
                name: "upload",
              });
            })
            .catch(() => {
              // on cancel
            });
          return;
        }
        if (this.userMsg.userType == 2 && item.adUrl == vipGoodsNo) {
          Toast("您已经是尊贵的湖南中石油会员啦~");
          return;
        }
        if (this.userMsg.userType == 0 && item.adUrl == vipGoodsNo) {
          window.localStorage.setItem("whatBuy", "vip");
          this.$router.push({
            name: "creatOrder",
            query: {
              goodsNo: item.adUrl,
              didPay: "noYet",
            },
          });
          return;
        }
        this.$router.push({
          name: "creatOrder",
          query: {
            goodsNo: item.productUrl,
            didPay: "noYet",
            isGsq: false,
          },
        });
      }
    },
    // 更多权限/查看全部
    watchAllOrMore(isRight) {
      if (window.localStorage.getItem("phone") == "") {
        this.$router.push({
          name: "getUser",
          query: {
            spid: "poster",
          },
        });
      }
      if (isRight) {
        this.$router.push({
          name: "oilShow",
          query: { flag: 1 },
        });
      } else {
        this.$router.push({
          name: "poster",
          query: { flag: 0 },
        });
      }
    },
    toJSON() {
      return this;
    },
  },
};
</script>

<style lang='scss' scoped>
#home {
  background-image: url(https://tsyx-rights-and-interests.oss-cn-hangzhou.aliyuncs.com/background/indexBG.png);
  background-size: 100%;
  background-repeat: no-repeat;
  background-color: #fff;
  width: 100%;
  padding-bottom: 3rem;
  padding-top: 0.2rem;
}
// 顶部
.top {
  position: relative;
  width: 100%;
  height: 1rem;
  line-height: 1rem;
  padding: 0 0.3rem;
  box-sizing: border-box;
  font-size: 0.4rem;
  color: #ffffff;
  //   定位
  .address {
    position: absolute;
    left: 0.3rem;
    top: 0;
    img {
      position: absolute;
      top: 50%;
      left: 0;
      transform: translateY(-50%);
      width: 0.4rem;
    }
    span {
      position: relative;
      left: 0.5rem;
      font-size: 0.3rem;
    }
  }
  //   搜索
  .search {
    position: absolute;
    top: 50%;
    transform: translateY(-50%);
    left: 2rem;
    width: 50%;
    height: 0.6rem;
    border: 1px solid #ffffff;
    background: rgba(255, 255, 255, 0.35);
    border-radius: 31px;
    line-height: 0.6rem;
    font-size: 0.3rem;
    padding: 0 0.7rem 0 0.8rem;
    img {
      position: absolute;
      top: 50%;
      left: 0.2rem;
      transform: translateY(-50%);
      width: 0.3rem;
    }
  }
  //   签到
  .signIn {
    img {
      position: absolute;
      top: 50%;
      right: 0.3rem;
      transform: translateY(-50%);
      width: 0.8rem;
    }
  }
}
// 会员卡
.memberBox {
  // 是会员
  .ismember {
    background-image: url(https://tsyx-rights-and-interests.oss-cn-hangzhou.aliyuncs.com/background/yesmember.png);
    box-shadow: 0px 8px 10px 5px rgba(230, 28, 28, 0.1);
  }
  //   不是会员
  .nomember {
    background-image: url(https://tsyx-rights-and-interests.oss-cn-hangzhou.aliyuncs.com/background/nomember.png);
    box-shadow: 0px 8px 10px 5px rgba(0, 0, 0, 0.1);
    border-radius: 0.25rem;
  }
  //   会员卡
  .member {
    position: relative;
    width: 90%;
    height: 4rem;
    background-repeat: no-repeat;
    background-size: 100% 100%;
    margin: 0.5rem auto;
    .logo {
      position: relative;
      top: 0.1rem;
      left: 0.3rem;
      width: 25%;
    }
    .watchAll {
      position: absolute;
      right: 0.5rem;
      top: 0.2rem;
      padding-right: 0.3rem;
      color: #f0aeb0;
    }
    .noColor {
      color: #000;
    }
    .icon-v-right {
      position: absolute;
      right: 0.1rem;
      top: 50%;
      width: 4px;
      height: 4px;
      border: 1px solid #f0aeb0;
      margin-top: -2.5px;
      border-width: 1px 1px 0 0;
      transform: rotate(45deg);
    }
    .noBorderColor {
      border: 1px solid #000;
      border-width: 1px 1px 0 0;
      transform: rotate(45deg);
    }
    .time {
      position: absolute;
      left: 0.9rem;
      bottom: 0.1rem;
      color: #fed7d8;
    }

    .memberButton {
      position: absolute;
      right: 0.3rem;
      bottom: 0.2rem;
      width: 1.8rem;
      height: 0.8rem;
      background-image: url(https://tsyx-rights-and-interests.oss-cn-hangzhou.aliyuncs.com/background/yesbutton.png);
      background-repeat: no-repeat;
      background-size: 100% 100%;
      line-height: 0.85rem;
      text-align: center;
      font-size: 0.3rem;
      font-weight: 700;
      color: #bc1b31;
    }

    .nobg {
      color: #000;
      background-image: url(https://tsyx-rights-and-interests.oss-cn-hangzhou.aliyuncs.com/background/nobutton.png);
    }
  }
  // 会员按钮
  .memberTab {
    width: 90%;
    margin: 0 auto;
    .tabs {
      text-align: center;
      margin-bottom: 0.2rem;
      img {
        width: 1rem;
        height: 1rem;
      }
      p {
        margin: 0;
      }
    }
    .child {
      width: 40px;
      height: 40px;
      background: #f2f3f5;
      border-radius: 4px;
    }
  }
}
// 轮播图
.banner {
  width: 90%;
  margin: auto;
  .bannerImg {
    width: 100%;
    img {
      width: 100%;
      height: 3rem;
    }
  }
}
// 热门商品
.hotShopTitle {
  width: 90%;
  margin: 0.5rem auto 0.1rem;
  color: #333333;
  font-size: 0.4rem;
}
.hotShopBox {
  width: 90%;
  margin: 0.3rem auto;
  white-space: nowrap;
  overflow-x: scroll;
  overflow-y: hidden;
  .hotShop {
    width: 35%;
    // overflow: hidden;
    // float: left;
    display: inline-block;
    margin-right: 0.5rem;
    // padding: 0.1rem;
    border: 1px solid #f3f3f3;
    border-radius: 12px;
    overflow: hidden;
    /deep/ .van-badge--fixed {
      top: 0.1rem;
    }
    .title {
      margin: 0;
      overflow: hidden; /*超出隐藏*/
      white-space: nowrap; /*强制在同一行显示*/
      text-overflow: ellipsis; /*省略号*/
      font-weight: 400;
      text-align: left;
      font-size: 0.35rem;
      color: #353333;
    }
    .msg {
      overflow: hidden; /*超出隐藏*/
      white-space: nowrap; /*强制在同一行显示*/
      text-overflow: ellipsis; /*省略号*/
      margin: 0;
      color: #a7a4a4;
      font-size: 0.25rem;
    }
    .price {
      color: red;
      span {
        font-size: 14px;
      }
      i {
        font-style: normal;
        font-size: 20px;
      }
    }
  }
}
.hotShopBox::-webkit-scrollbar {
  width: 0 !important;
}
.hotShopBox {
  -ms-overflow-style: none;
}
.hotShopBox {
  overflow: -moz-scrollbars-none;
}

// 热门推荐等
.hotList {
  width: 90%;
  margin: auto;
  .box {
    display: inline-block;
    width: 25%;
    text-align: center;
    padding: 0.3rem 0;
    box-sizing: border-box;
    .van-badge__wrapper {
      display: block;
      margin: 0.3rem auto 0.2rem;
      width: 50%;
    }
    img {
      width: 100%;
      margin-bottom: 0.1rem;
    }
    .van-badge {
      border-radius: 999px 999px 999px 0;
    }
    .fiveSela {
      position: absolute;
      // top: -1rem;
      width: 80%;
      left: 50%;
      font-size: 0.25rem;
      color: #ffffff;
      transform: translateX(-50%) translateY(-50%);
      background: linear-gradient(90deg, #ff902d, #ffb65a);
      border-radius: 0 0.3rem 0.3rem 0.3rem;
    }
  }
}
.hotBigBox {
  margin: 0.5rem 0;
  border: 1px solid #ebedf0;
  border-radius: 0.3rem;
  box-shadow: 0px 8px 10px 0px rgba(0, 0, 0, 0.1);
}
[class*="van-hairline"]::after {
  border: none;
}
/deep/.van-badge.van-badge--fixed {
  border-radius: 100px 100px 100px 0px;
}
// 猜你喜欢
.youlike {
  position: relative;
  width: 100%;
  height: 3rem;
  margin-bottom: .3rem;
  img {
    position: absolute;
    width: 110%;
    left: 50%;
    top: 50%;
    transform: translateX(-50%) translateY(-50%);
  }
}
</style>
